package cn.iocoder.yudao.framework.mybatis.autotable.config;

import cn.hutool.extra.spring.SpringUtil;
import cn.iocoder.yudao.framework.mybatis.autotable.creator.TableAutoCreator;
import cn.iocoder.yudao.framework.mybatis.autotable.ddl.DDLGenerator;
import cn.iocoder.yudao.framework.mybatis.autotable.ddl.DefaultDDLGenerator;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.sql.DataSource;

@Configuration
public class AutoTableConfiguration {

    @Bean
    @ConditionalOnMissingBean
    public DDLGenerator ddlGenerator() {
        return new DefaultDDLGenerator(); // 用户可自定义实现
    }

    @Bean
    public TableAutoCreator tableAutoCreator(DDLGenerator ddlGenerator, @Value("${spring.autotable.base-packages:}") String[] basePackages) {
        DataSource dataSource = SpringUtil.getBean(DataSource.class);
        return new TableAutoCreator(dataSource, ddlGenerator, basePackages);
    }
}